<?php
/* -------------------------------------------------------------------------------------
* 	ID:						$Id: brand.php 22 2013-09-21 11:53:27Z phone.mueller@googlemail.com $
* 	Letzter Stand:			$Revision: 22 $
* 	zuletzt geaendert von:	$Author: siekiera $
* 	Datum:					$Date: 2013-09-21 11:53:27 +0000 (Sat, 21 Sep 2013) $
*
* 	SEO:mercari by Siekiera Media
* 	http://www.seo-mercari.de
*
* 	Copyright (c) since 2011 SEO:mercari
* --------------------------------------------------------------------------------------
* 	based on:
* 	(c) 2000-2001 The Exchange Project  (earlier name of osCommerce)
* 	(c) 2002-2003 osCommerce - www.oscommerce.com
* 	(c) 2003     nextcommerce - www.nextcommerce.org
* 	(c) 2005     xt:Commerce - www.xt-commerce.com
*
* 	Released under the GNU General Public License
* ----------------------------------------------------------------------------------- */

require('includes/application_top.php');

switch ($_GET['action']) {
	case 'insert':
	case 'save':
		$brand_id = $_GET['bID'];
		$brand_name = $_POST['brand_name'];

		$sql_data_array = array('brand_name' => $brand_name);

		if ($_GET['action'] == 'insert') {
			$insert_sql_data = array('date_added' => 'now()');
			$sql_data_array = array_merge($sql_data_array, $insert_sql_data);
			$db->db_perform(TABLE_BRAND, $sql_data_array);
			$brand_id = $db->db_insert_id();

		} elseif($_GET['action'] == 'save') {
			$update_sql_data = array('last_modified' => 'now()');
			$sql_data_array = array_merge($sql_data_array, $update_sql_data);
			$db->db_perform(TABLE_BRAND, $sql_data_array, 'update', "brand_id = ".$db->db_prepare($brand_id));
		}
		
		if(isset($_POST['del_pic'])) {
			@unlink(DIR_FS_CATALOG_IMAGES.'/'.$_POST['del_pic']);
			$db->db_query("UPDATE ".TABLE_BRAND." SET brand_image = '' WHERE brand_id = ".$db->db_prepare($brand_id));
		}
		
		$dir_brand = DIR_FS_CATALOG_IMAGES.'/brand';
		if($brand_image = &try_upload('brand_image', $dir_brand)) {
			if($_GET['action'] == 'save')
				@unlink(DIR_FS_CATALOG_IMAGES.'/'.$_POST['old_brand_image']);
				
			$db->db_query("UPDATE ".TABLE_BRAND." SET brand_image = 'brand/".$brand_image->filename."' WHERE brand_id = ".$db->db_prepare($brand_id));
		}

		$languages = get_languages();
		for ($i = 0, $n = sizeof($languages); $i < $n; $i++) {
			$brand_url_array = $_POST['brand_url'];
			$brand_meta_title_array = $_POST['brand_meta_title'];
			$brand_meta_description_array = $_POST['brand_meta_description'];
			$brand_meta_keywords_array = $_POST['brand_meta_keywords'];
			
			$language_id = $languages[$i]['id'];

			$sql_data_array = array('brand_url' => $brand_url_array[$language_id],
									'brand_description' => stripslashes($_POST['brand_description_'.$language_id]),
									'brand_meta_keywords' => $brand_meta_keywords_array[$language_id],
									'brand_meta_title' => $brand_meta_title_array[$language_id],
									'brand_meta_description' => $brand_meta_description_array[$language_id]);
									
			if ($_GET['action'] == 'insert') {
				$insert_sql_data = array('brand_id' => $brand_id,
										'languages_id' => $language_id);
				$sql_data_array = array_merge($sql_data_array, $insert_sql_data);
				$db->db_perform(TABLE_BRAND_INFO, $sql_data_array);

			} elseif ($_GET['action'] == 'save')
				$db->db_perform(TABLE_BRAND_INFO, $sql_data_array, 'update', "brand_id = ".$db->db_prepare($brand_id)." and languages_id = '".$language_id."'");
		}

		redirect(href_link(FILENAME_BRAND, 'bID='.$brand_id.(isset($_GET['page'])?'&page='.$_GET['page']:'')));
	break;

	case 'deleteconfirm':
		$brand_id = $_GET['bID'];

		if ($_POST['delete_image'] == 'on') {
			$brand = $db->db_query("SELECT brand_image FROM ".TABLE_BRAND." WHERE brand_id = ".$db->db_prepare($brand_id));

			if(file_exists(DIR_FS_CATALOG_IMAGES.$brand->fields['brand_image']))
				@unlink(DIR_FS_CATALOG_IMAGES.$brand->fields['brand_image']);
		}

		$db->db_query("DELETE FROM ".TABLE_BRAND." WHERE brand_id = ".$db->db_prepare($brand_id));
		$db->db_query("DELETE FROM ".TABLE_BRAND_INFO." WHERE brand_id = ".$db->db_prepare($brand_id));

		$db->db_query("UPDATE ".TABLE_PRODUCTS." SET brand_id = '' WHERE brand_id = ".$db->db_prepare($brand_id));

		if (USE_CACHE == 'true')
			reset_cache_block('brand');

		redirect(href_link(FILENAME_BRAND, (isset($_GET['page']) ? 'page='.$_GET['page'] : '')));
	break;
}
require(DIR_WS_INCLUDES.'metatag.php');
?>
<script type="text/javascript">
	function checkName(){if($('input[name=brand_name]').val()==''){alert('Geben Sie einen Hersteller/Markennamen ein.');$('input[name=brand_name]').css({'border':'1px solid #b20000'}).focus();return false;}else{return true;}}
</script>
</head>
<body>
<?php require(DIR_WS_INCLUDES.'header.php'); ?>
<div id="wrapper">
<table class="outerTable" cellpadding="0" cellspacing="0">
  <tr>
    <td class="columnLeft2" width="<?php echo BOX_WIDTH; ?>" valign="top">
		<?php require(DIR_WS_INCLUDES.'column_left.php'); ?>
	</td>
    <td class="boxCenter" valign="top"><table border="0" width="100%" cellspacing="0" cellpadding="2">
      <tr>
        <td width="100%">
			<table class="table_pageHeading" border="0" width="100%" cellspacing="0" cellpadding="0">
          		<tr>
            		<td class="pageHeading"><?php echo HEADING_TITLE; ?></td>
          		</tr>
        	</table>
		</td>
      </tr>
      <tr>
        <td><table border="0" width="100%" cellspacing="0" cellpadding="0">
          <tr>
            <td valign="top">
            <?php
				if(isset($_GET['action']) && $_GET['action'] == 'new') {
				echo '<p>'.TEXT_NEW_INTRO.'</p>';
				echo draw_form('brand', FILENAME_BRAND, 'action=insert', 'POST', 'enctype="multipart/form-data" onsubmit="return checkName()"');
			?>
				<table width="100%" cellspacing="0" cellpadding="0">
					<tr>
						<td class="main"><?php echo TEXT_BRAND_NAME ?></td>
						<td class="main"><?php echo draw_input_field('brand_name', '', 'style="width:100%"'); ?></td>
					</tr>
					<tr>
						<td class="main"><?php echo TEXT_BRAND_IMAGE ?></td>
						<td class="main"><?php echo draw_file_field('brand_image'); ?></td>
					</tr>
					<?php 
					$languages = get_languages();
				     for ($i = 0, $n = sizeof($languages); $i < $n; $i++) {
					?>
						<tr>
							<td class="main"><nobr><?php echo image(DIR_WS_LANGUAGES.$languages[$i]['directory'].'/'.$languages[$i]['image'], $languages[$i]['name']).' '.TEXT_BRAND_URL; ?></nobr></td>
							<td class="main">
								<?php echo draw_input_field('brand_url['.$languages[$i]['id'].']', '', 'style="width:100%"') ?>
							</td>
						</tr>
						<tr>
							<td valign="top" class="main"><?php echo image(DIR_WS_LANGUAGES.$languages[$i]['directory'].'/'.$languages[$i]['image'], $languages[$i]['name']).' '.TEXT_BRAND_DESC; ?></td>
							<td class="main">
								<?php 
									echo wysiwyg('brand_description_'.$languages[$i]['id']);
								?>
							</td>
						</tr>
						<tr>
							<td class="main">Meta-Titel:</td>
							<td class="main"><?php echo draw_input_field('brand_meta_title['.$languages[$i]['id'].']', '', 'style="width:100%"') ?></td>
						</tr>
						<tr>
							<td class="main">Meta-Description:</td>
							<td class="main"><?php echo draw_input_field('brand_meta_description['.$languages[$i]['id'].']', '', 'style="width:100%"') ?></td>
						</tr>
						
						<tr>
							<td class="main">Meta-Keywords:</td>
							<td class="main"><?php echo draw_input_field('brand_meta_keywords['.$languages[$i]['id'].']', '', 'style="width:100%"') ?></td>
						</tr>
					<?php } ?>
				      <tr>
				      	<td>
				      		<?php echo button_link(BUTTON_CANCEL, href_link(FILENAME_BRAND, (isset($_GET['page']) ? 'page='.$_GET['page'] : ''))) ?>
				      	</td>
				      	<td align="right">
				      		<?php echo button(BUTTON_SAVE);?>
				      	</td>
				      </tr>
					</tr>
				</table>
				
				</form>
			<?php
				} elseif(isset($_GET['action']) && $_GET['action'] == 'edit') {
				
				$data = $db->db_query("SELECT
											brand_image,
											brand_name
										FROM
											".TABLE_BRAND."
										WHERE
											brand_id = '".$_GET['bID']."'");
				
				echo '<p>Bearbeiten von <em><b>"'.$data->fields['brand_name'].'"</b></em></p>';
				echo draw_form('brand', FILENAME_BRAND, 'action=save&bID='.$_GET['bID'], 'POST', 'enctype="multipart/form-data" onsubmit="return checkName()"');
			?>
				<table width="100%" cellspacing="0" cellpadding="0">
					<tr>
						<td class="main"><?php echo TEXT_BRAND_NAME ?></td>
						<td class="main"><?php echo draw_input_field('brand_name', $data->fields['brand_name'], 'style="width:100%"'); ?></td>
					</tr>
					<tr>
						<td class="main" valign="top"><?php echo TEXT_BRAND_IMAGE ?></td>
						<td class="main">
							<?php echo draw_file_field('brand_image').draw_hidden_field('old_brand_img', $data->fields['brand_image']);
							if(!empty($data->fields['brand_image']) && file_exists('../images/'.$data->fields['brand_image'])) {
								echo '<br /><img src="../images/'.$data->fields['brand_image'].'" alt="" /><br />';
								echo draw_selection_field('del_pic', 'checkbox', $data->fields['brand_image']).' '.TEXT_DELETE;
								echo draw_hidden_field('old_brand_image', $data->fields['brand_image']);
							}	
							?>
						</td>
					</tr>
					<?php 
					$languages = get_languages();
				     for ($i = 0, $n = sizeof($languages); $i < $n; $i++) {
				     	$l_brand = $db->db_query("SELECT 
				     									brand_meta_title,
				     									brand_meta_description,
				     									brand_meta_keywords,
				     									brand_description,
				     									brand_url
				     								FROM
				     									".TABLE_BRAND_INFO."
				     								WHERE
				     									languages_id = '".$languages[$i]['id']."'
				     								AND
				     									brand_id = '".$_GET['bID']."' ");
					?>
						<tr>
							<td class="main"><nobr><?php echo image(DIR_WS_LANGUAGES.$languages[$i]['directory'].'/'.$languages[$i]['image'], $languages[$i]['name']).' '.TEXT_BRAND_URL; ?></nobr></td>
							<td class="main">
								<?php echo draw_input_field('brand_url['.$languages[$i]['id'].']', $l_brand->fields['brand_url'], 'style="width:100%"') ?>
							</td>
						</tr>
						<tr>
							<td valign="top" class="main"><?php echo image(DIR_WS_LANGUAGES.$languages[$i]['directory'].'/'.$languages[$i]['image'], $languages[$i]['name']).' '.TEXT_BRAND_DESC; ?></td>
							<td class="main">
								<?php echo wysiwyg('brand_description_'.$languages[$i]['id'], $l_brand->fields['brand_description']); ?>
							</td>
						</tr>
						<tr>
							<td class="main">Meta-Titel:</td>
							<td class="main"><?php echo draw_input_field('brand_meta_title['.$languages[$i]['id'].']', $l_brand->fields['brand_meta_title'], 'style="width:100%"') ?></td>
						</tr>
						<tr>
							<td class="main">Meta-Description:</td>
							<td class="main"><?php echo draw_input_field('brand_meta_description['.$languages[$i]['id'].']', $l_brand->fields['brand_meta_description'], 'style="width:100%"') ?></td>
						</tr>
						
						<tr>
							<td class="main">Meta-Keywords:</td>
							<td class="main"><?php echo draw_input_field('brand_meta_keywords['.$languages[$i]['id'].']', $l_brand->fields['brand_meta_keywords'], 'style="width:100%"') ?></td>
						</tr>
					<?php } ?>
				      <tr>
				      	<td>
				      		<?php echo button_link(BUTTON_CANCEL, href_link(FILENAME_BRAND, (isset($_GET['page']) ? 'page='.$_GET['page'] : ''))) ?>
				      	</td>
				      	<td align="right">
				      		<?php echo button(BUTTON_SAVE);?>
				      	</td>
				      </tr>
					</tr>
				</table>
				
				</form>
			<?php
				} else {
			?>	
            <table width="100%" cellspacing="0" cellpadding="0" class="dataTable">
              <tr class="dataTableHeadingRow">
                <td class="dataTableHeadingContent"><?php echo TABLE_HEADING_BRAND; ?></td>
                <td class="dataTableHeadingContent" align="right"><?php echo TABLE_HEADING_ACTION; ?>&nbsp;</td>
              </tr>
				<?php 
				  $sql = "SELECT brand_id, brand_name, brand_image, date_added, last_modified FROM ".TABLE_BRAND." ORDER BY brand_name";

				  $query = page_break_create($_POST['per_site'], $_GET['page'], $sql, 'brand.php');

				  $brand = $query['query'];

				  while (!$brand->EOF) { $rows++;
				    if (((!$_GET['bID']) || (@$_GET['bID'] == $brand->fields['brand_id'])) && (!$mInfo) && (substr($_GET['action'], 0, 3) != 'new')) {
				      $brand_products = $db->db_query("SELECT COUNT(*) AS products_count FROM ".TABLE_PRODUCTS." WHERE brand_id = '".$brand->fields['brand_id']."'");

				      $mInfo_array = array_merge($brand->fields, $brand_products->fields);
				      $mInfo = new objectInfo($mInfo_array);
				    }

				    if ( (is_object($mInfo)) && ($brand->fields['brand_id'] == $mInfo->brand_id) ) {
				      echo '              <tr class="dataTableRowSelected" onmouseover="this.style.cursor=\'pointer\'" onclick="document.location.href=\''.href_link(FILENAME_BRAND, (isset($_GET['page']) ? 'page='.$_GET['page'] : '').'&bID='.$brand->fields['brand_id'].'&action=edit').'\'">'."\n";
				    } else {
						if ($rows % 2 == 0)
				    		$f = 'dataTableRow';
				    	else
				    		$f = '';
				      echo '<tr class="'.$f.'" onmouseover="this.className=\'dataTableRowOver\';this.style.cursor=\'pointer\'" onmouseout="this.className=\''.$f.'\'" onclick="document.location.href=\''.href_link(FILENAME_BRAND, (isset($_GET['page']) ? 'page='.$_GET['page'] : '').'&bID='.$brand->fields['brand_id']).'\'">'."\n";
				    }
				?>
                <td class="dataTableContent"><?php echo $brand->fields['brand_name']; ?></td>
                <td class="dataTableContent" align="right">
                <?php
                if ( (is_object($mInfo)) && ($brand->fields['brand_id'] == $mInfo->brand_id) ) {
                	echo image(DIR_WS_IMAGES.'icon_arrow_right.gif');
                } else {
                	echo '<a href="'.href_link(FILENAME_BRAND, 'bID='.$brand->fields['brand_id'].(isset($_GET['page']) ? '&page='.$_GET['page'] : '')).'">'.image(DIR_WS_IMAGES.'icon_info.gif', IMAGE_ICON_INFO).'</a>';
                	echo ' <a href="'.href_link(FILENAME_BRAND, 'bID='.$brand->fields['brand_id'].'&action=edit'.(isset($_GET['page']) ? '&page='.$_GET['page'] : '')).'">'.image(DIR_WS_IMAGES.'icon_edit.gif', IMAGE_ICON_EDIT).'</a>';
                } ?>&nbsp;</td>
			<?php
				echo '</tr>';
				$brand->MoveNext();
			  }
			?>
            </table>
            <table border="0" width="100%" cellspacing="0" cellpadding="2">
             <tr class="page_break">
					<td class="smallText" valign="top" width="33.3%"><?php echo 'Angezeigt werden '.$query['from'].' bis '.$query['to'].' (von insgesamt '.$query['total'].' Herstellern)'; ?></td>
					<td class="smallText" align="center" width="33.3%"></td>
					<td class="smallText" align="right" width="33.3%">
						Hersteller pro Seite: <?php echo $query['page_break']->perSiteDropdown($_GET['page']); ?>
					</td>
				</tr>
				<tr><td align="center" colspan="3"><?php echo '<br />'.$query['links']; ?></td></tr>
            </table>
            <table width="100%">
            	<?php
				  if ($_GET['action'] != 'new') {
				?>
              <tr>
                <td align="right" colspan="2" class="smallText"><?php echo button_link(BUTTON_INSERT, href_link(FILENAME_BRAND, 'bID='.$mInfo->brand_id.'&action=new'.(isset($_GET['page']) ? '&page='.$_GET['page'] : ''))); ?></td>
              </tr>
			<?php
			  }
			  }
			?>
            </table>
            </td>
				<?php
				 switch ($_GET['action']) {
				 	case 'delete':
						$heading[] = array('text' => '<b>'.TEXT_HEADING_DELETE_BRAND.'</b>');
						
						$contents = array('form' => draw_form('brand', FILENAME_BRAND, 'bID='.$mInfo->brand_id.'&action=deleteconfirm'.(isset($_GET['page']) ? '&page='.$_GET['page'] : '')));
						$contents[] = array('text' => TEXT_DELETE_INTRO);
						$contents[] = array('align' => 'center' ,'text' => '<br /><b>'.$mInfo->brand_name.'</b>');
						$contents[] = array('align' => 'center' ,'text' => '<br />'.draw_checkbox_field('delete_image', '', true).' '.TEXT_DELETE_IMAGE);
						
						$contents[] = array('align' => 'center', 'text' => '<br />'.button(BUTTON_DELETE).' '.button_link(BUTTON_CANCEL, href_link(FILENAME_BRAND, '&bID='.$mInfo->brand_id.(isset($_GET['page']) ? '&page='.$_GET['page'] : ''))));
						break;
					
					default :
						if(is_object($mInfo)) {
							$heading = array();
							$contents = array();
							$heading[] = array('text' => '<b>'.$mInfo->brand_name.'</b>');
							
							$contents[] = array('align' => 'center', 'text' => button_link(BUTTON_EDIT, href_link(FILENAME_BRAND, 'bID='.$mInfo->brand_id.'&action=edit'.(isset($_GET['page']) ? '&page='.$_GET['page'] : ''))).'&nbsp;'.button_link(BUTTON_DELETE, href_link(FILENAME_BRAND, 'bID='.$mInfo->brand_id.'&action=delete'.(isset($_GET['page']) ? '&page='.$_GET['page'] : ''))));
							$contents[] = array('text' => '<br />'.TEXT_DATE_ADDED.' '.date_short($mInfo->date_added));
							if (not_null($mInfo->last_modified)) $contents[] = array('text' => TEXT_LAST_MODIFIED.' '.date_short($mInfo->last_modified));
							$contents[] = array('text' => '<br />'.info_image($mInfo->brand_image, $mInfo->brand_name));
							$contents[] = array('text' => '<br />'.TEXT_PRODUCTS.' '.$mInfo->products_count);
						}
						break;
				}
				if((not_null($heading)) && (not_null($contents)) ) {
				    echo '<td width="25%" class="border" valign="top">'."\n";
				    $box = new box;
				    echo $box->infoBox($heading, $contents);
				    echo '</td>'."\n";
				}
			?>
          </tr>
        </table></td>
      </tr>
    </table></td>
  </tr>
</table>
</div>
<?php require(DIR_WS_INCLUDES.'footer.php'); ?>
</body>
</html>
<?php require(DIR_WS_INCLUDES.'application_bottom.php'); ?>